import time

from selenium import webdriver
from action import elementAction
from config.Varconfig import testdataPath, chromePath
from until import find_element
from until.ExcelOperate import ExcelOperate
from until.generate_func import generate_func_express
from scripts import excel


def add_person(datasource_sheet,step_sheet):
    try:
        data_row_nums = excel.get_row_nums(datasource_sheet)
        step_row_nums = excel.get_row_nums(step_sheet)

        # success rows
        success_record = 0

        # need rows
        need_record = 0

        for i in range(2,data_row_nums+1):

            if excel.get_cell_values(i,6,datasource_sheet).lower() == 'y':
                need_record += 1
                name = excel.get_cell_values(i,1,datasource_sheet)
                mail = excel.get_cell_values(i,2,datasource_sheet)
                phone = excel.get_cell_values(i,3,datasource_sheet)
                is_start = excel.get_cell_values(i,4,datasource_sheet)
                remark = excel.get_cell_values(i,5,datasource_sheet)
                # print(f'{name}-{mail}-{phone}-{is_start}-{remark}')

                # success step
                success_step = 0
                for j in range(2,step_row_nums+1):
                    step_desc = excel.get_cell_values(j,2,step_sheet)
                    key_word = excel.get_cell_values(j,3,step_sheet)
                    location_type = excel.get_cell_values(j,4,step_sheet)
                    location_express = excel.get_cell_values(j,5,step_sheet)
                    operate_value = excel.get_cell_values(j,6,step_sheet)
                    # print(f'{step_desc}-{key_word}-{location_type}-{location_express}-{operate_value}')

                    # replace operate value
                    if isinstance(operate_value,str) and '$' in operate_value and '{' in operate_value and '}' in operate_value:
                        operate_value = eval(operate_value[2:operate_value.index('}')])
                        # print(operate_value)

                    method_express = generate_func_express(key_word,operate_value,location_type,location_express)


                    if operate_value != 'b':
                        try:
                            print(f'start action {step_desc}')
                            eval(method_express)
                        except Exception as e:
                            print(f'action {step_desc} error {e}')

                        else:
                            # no error
                            success_step += 1
                            print(f'action {step_desc} success')
                    else:
                        # no star
                        success_step += 1
                if success_step+1 == step_row_nums:
                    success_record += 1
                    excel.write_value(i,7,'pass',testdataPath,datasource_sheet)
                else:
                    excel.write_value(i, 7, 'fail', testdataPath, datasource_sheet)

        if success_record == need_record:
            return 'success'
        return 'fail'



    except Exception as e:
        print(e)


if __name__ == '__main__':
    excel = ExcelOperate()
    excel.load_workbook(testdataPath)
    datasource_sheet = excel.get_sheet_by_name('add_person')
    step_sheet = excel.get_sheet_by_name('contract')
    # add_person(excel,datasource_sheet,step_sheet)

    wd = webdriver.Chrome(chromePath)
    wd.get('http://mail.163.com')
    wd.maximize_window()
    time.sleep(2)
    iframe = find_element(wd,'tag name','iframe')
    wd.switch_to.frame(iframe)
    find_element(wd,'name','email').send_keys('13037521553')
    find_element(wd,'name','password').send_keys('Ruru999999')
    find_element(wd,'id','dologin').click()
    time.sleep(3)
    elementAction.wd = wd
    add_person(datasource_sheet,step_sheet)
